package mytest.controller;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import mytest.util.RequestUtil;
import mytest.util.Response;
import test.testcase.dao.JobDao;
import test.testcase.dao.TimerDao;

@Controller @RequestMapping(value = "/timers", produces = "application/json; charset=utf-8")
@CrossOrigin
public class timers {
	
	@RequestMapping(value = "/getlist")
	public @ResponseBody String getlist(HttpServletRequest request) {
		String userid = RequestUtil.getProjectId(request);
		return TimerDao.getTimerList(userid).toResponse();
	}
	
	@RequestMapping(value = "/excute/{id}")
	public @ResponseBody String getlist(@PathVariable String id) {
		JobDao.addAnPenddingJob(id);
		return TimerDao.setTimerExcuted(id).toResponse();
	}
	
	@RequestMapping(value = "/add")
	public @ResponseBody String add(HttpServletRequest request, @RequestBody String body) {
		String userid = RequestUtil.getUserId(request);
		JSONObject object = new JSONObject(body);
		String jobid = object.getString("jobid");
		String projectid = RequestUtil.getProjectId(request);
		String title = object.getString("title");
		String time = object.getString("xiaoshi")+":"+object.getString("fenzhong")+":00";
		if (!"".equals(jobid)) {
			projectid = "null";
		}else {
			jobid = "null";
		}
		String days = "";
		for(int i=1;i<=7;i++) {
			if (object.has(("周"+i)) && object.getString("周"+i).equals("on")) {
				if (days.equals("")) {
					days = i+"";
				}else {
					days += ","+i;
				}
			}
		}
		if (days.equals("")) {
			Response response = new Response();
			return response.setFalse("请选择执行日").toString();
		}
		return TimerDao.add(userid, title, jobid, projectid, days, time).toResponse();
	}
	
	@RequestMapping(value = "/save/{id}")
	public @ResponseBody String save(@PathVariable String id, @RequestBody String body, HttpServletRequest request) {
		JSONObject object = new JSONObject(body);
		String jobid = object.getString("jobid");
		String projectid = RequestUtil.getProjectId(request);
		String title = object.getString("title");
		String time = object.getString("xiaoshi")+":"+object.getString("fenzhong")+":00";
		if (!"".equals(jobid)) {
			projectid = "null";
		}else {
			jobid = "null";
		}
		String days = "";
		for(int i=1;i<=7;i++) {
			if (object.has(("周"+i)) && object.getString("周"+i).equals("on")) {
				if (days.equals("")) {
					days = i+"";
				}else {
					days += ","+i;
				}
			}
		}
		if (days.equals("")) {
			Response response = new Response();
			return response.setFalse("请选择执行日").toString();
		}
		return TimerDao.save(id, title, jobid, projectid, days, time).toResponse();
	}
	
	@RequestMapping(value = "/getinfo/{id}")
	public @ResponseBody String info(@PathVariable String id) {
		return TimerDao.getInfo(id).toResponse();
	}
	
	@RequestMapping(value = "/del/{id}")
	public @ResponseBody String del(@PathVariable String id) {
		return TimerDao.del(id).toResponse();
	}
}
